﻿using System;
using System.Collections.Generic;
using Engine.Core.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Engine.Core.Engines
{
    public class SimpleRenderEngine : IRenderEngine{
        public List<IExtendedDrawable> ObjectsToDraw;
        public List<Action<SpriteBatch>> Actions;

        public SimpleRenderEngine(){
            ObjectsToDraw = new List<IExtendedDrawable>();
            Actions = new List<Action<SpriteBatch>>();
        }

        public void Render(SpriteBatch spriteBatch, GameTime gameTime){
            if (Actions.Count == 0 && ObjectsToDraw.Count == 0)
                return;


            spriteBatch.Begin();
            ObjectsToDraw.ForEach(x => x.SetSpriteBatch(spriteBatch));
            ObjectsToDraw.ForEach(x => x.Draw(gameTime));
            Actions.ForEach(x => x.Invoke(spriteBatch));

            spriteBatch.End();

            ObjectsToDraw.Clear();
            Actions.Clear();
        }

        public void AddObjectsForRendering(List<IExtendedDrawable> drawables)
        {
            ObjectsToDraw.AddRange(drawables);
        }

        public void AddAdditionalRenderActions(params Action<SpriteBatch>[] actions){
            Actions.AddRange(actions);
        }
    }
}
